package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: WangYaWei
 * @description:
 * @create: 2019-08-13 21:29
 **/
@RestController
@RequestMapping(value = "/label")
@CrossOrigin
public class LabelController {
    @Autowired
    private LabelService labelService;
    @GetMapping
    public Result findAll(){
        return new Result(true, StatusCode.OK, "查询成功", labelService.findAll());
    }

    @GetMapping(value = "/search/{page}/{size}")
    public Result findSearch(@RequestBody Map<String, Object> searchMap, @PathVariable int page, @PathVariable int size){
        Page<Label> pageData = labelService.findSearch(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Label>(pageData.getTotalElements(), pageData.getContent()));
    }

    @PostMapping(value = "/search")
    public Result findSearch(@RequestBody Map<String, Object> searchMap){
        List<Label> list = labelService.findSearch(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping(value = "/{id}")
    public Result findById(@PathVariable String id){
        System.out.println("no:2");
        return new Result(true, StatusCode.OK, "查询成功", labelService.findById(id));
    }

    @PostMapping
    public Result save(@RequestBody Label label){
        labelService.add(label);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @RequestBody Label label){
        label.setId(id);
        labelService.update(label);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping(value = "/{id}")
    public Result deleteById(@PathVariable String id){
        labelService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }


}
